package com.qms.repast.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;

import com.orhanobut.logger.Logger;
import com.qms.repast.R;
import com.qms.repast.entity.MessageEvent;
import com.qms.repast.entity.resbean.UserInfo;
import com.qms.repast.ui.base.BaseActivity;
import com.qms.repast.ui.presenter.LoginPresenter;
import com.qms.repast.ui.view.ILoginView;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jiguang.share.android.api.AuthListener;
import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.PlatActionListener;
import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.api.ShareParams;
import cn.jiguang.share.android.model.AccessTokenInfo;
import cn.jiguang.share.android.model.BaseResponseInfo;

/**
 * @author 泅渡者
 * @date 2018/8/31 0031
 * @describe: 登录界面
 */
public class LoginActivity extends BaseActivity<LoginPresenter> implements ILoginView, AuthListener {
	@BindView(R.id.btn_test)
	Button btnTest;
	@BindView(R.id.btn_wxLogin)
	Button btnWxLogin;
	@BindView(R.id.btn_wxInfo)
	Button btnWxInfo;
	@BindView(R.id.btn_share)
	Button btnShare;
	/**
	 * 待授权的列表
	 */
	private List<String> platformList;
	public static String share_url = "https://www.jiguang.cn";
	public static String share_text = "JShare SDK支持主流社交平台、帮助开发者轻松实现社会化功能！";
	public static String share_title = " 欢迎使用极光社会化组件JShare";

	@Override
	protected int initLayout () {
		return R.layout.activity_login;
	}

	@Override
	protected void initPresenter (Intent intent) {
		mPresenter = new LoginPresenter(this, this);
	}

	@Override
	protected void initView () {
		platformList = JShareInterface.getPlatformList();
	}

	@Override
	protected void initData () {

	}

	@Override
	public void showInfo (UserInfo userInfo) {
		EventBus.getDefault().post(new MessageEvent<>(1012, userInfo));
		finish();
	}

	@OnClick({R.id.btn_test, R.id.btn_wxLogin, R.id.btn_wxInfo,R.id.btn_share})
	public void onViewClicked (View view) {
		switch (view.getId()) {
			case R.id.btn_test:
				Map<String, String> params = new HashMap<>();
				params.put("loginId", "13259842697");
				params.put("loginPwd", "111111");
				params.put("orderNo", "");
				mPresenter.getLogin(params);
				break;
			case R.id.btn_wxLogin:
				JShareInterface.authorize(platformList.get(0), this);
				//取消授权
//				JShareInterface.removeAuthorize(platformList.get(0), this);
				break;
			case R.id.btn_wxInfo:
				JShareInterface.getUserInfo(platformList.get(0), this);
				break;
			case R.id.btn_share:
				ShareParams shareParams = new ShareParams();
				shareParams.setShareType(Platform.SHARE_WEBPAGE);
				shareParams.setTitle(LoginActivity.share_title);
				shareParams.setText(LoginActivity.share_text);
				shareParams.setUrl(LoginActivity.share_url);
				shareParams.setImagePath("");
				JShareInterface.share(platformList.get(0), shareParams, mShareListener);
				break;
			default:
				break;
		}
	}


	@Override
	public void onComplete (Platform platform, int action, BaseResponseInfo data) {
		Logger.d("onComplete:" + platform + ",action:" + action + ",data:" + data);
		//授权信息
		if (data instanceof AccessTokenInfo) {
			//token
			String token = ((AccessTokenInfo) data).getToken();
			//token有效时间，时间戳
			long expiration = ((AccessTokenInfo) data).getExpiresIn();
			//refresh_token
			String refreshToken = ((AccessTokenInfo) data).getRefeshToken();
			//openid
			String openid = ((AccessTokenInfo) data).getOpenid();
			//授权原始数据，开发者可自行处理
			String originData = data.getOriginData();
			Logger.d(originData);
		}
	}

	@Override
	public void onError (Platform platform, int action, int errorCode, Throwable error) {
		Logger.d("onComplete:" + platform + ",action:" + action + ",error:" + error);
	}

	@Override
	public void onCancel (Platform platform, int action) {
		Logger.d("onComplete:" + platform + ",action:" + action);
	}


	private PlatActionListener mShareListener = new PlatActionListener() {
		@Override
		public void onComplete (Platform platform, int action, HashMap<String, Object> data) {

		}

		@Override
		public void onError (Platform platform, int action, int errorCode, Throwable error) {

		}

		@Override
		public void onCancel (Platform platform, int action) {

		}
	};

}
